package test.gsn.portal;

import java.util.Timer;
import java.util.TimerTask;

import android.gsn.engine.utility.UtilityAndroid;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.engine.R;
import com.gsn.dataCommon.MobileData;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.game.PokerGame;

public class V2PlayPortalActivity extends AndroidApplication {
	protected static final String tag = "V2PlayPortalActivity";
	/** Called when the activity is first created. */
	RelativeLayout layout;
	public EditText nameUser;
	public EditText passUser;
	public EditText nameRoom;
	public EditText chat;
	public EditText gold;
	public EditText xu;
	public int heightText;
	public RelativeLayout loadingLayout;
	VideoView view;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);

		view = (VideoView) findViewById(R.id.videoView1);
		Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.intro);
		view.setVideoURI(video);
		// view.setMediaController(new MediaController(this));
		view.setMediaController(null);
		view.requestFocus();

		loadingLayout = new RelativeLayout(this);
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.loading_gsn, loadingLayout);

		AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
		config.useWakelock = true;
		createWakeLock(config);

		MobileData.device = android.os.Build.DEVICE;
		MobileData.osVersion = android.os.Build.VERSION.SDK;
		MobileData.name = android.os.Build.MANUFACTURER;

		Timer timer = new Timer();
		timer.schedule(new StartTask(), 2000);
		layout = (RelativeLayout) findViewById(R.id.layout);
		if (!ViewGame.getInstance().initGame) {
			PokerGame game = new PokerGame();
			MyPoker.game = game;
			ViewGame.getInstance().initGame = true;
			// MyPoker.client = new MercuryClient(MyPoker.server, 443,
			// MyPoker.game);
			// MyPoker.downloader = new DownloaderAndroid(this);
			ViewGame.getInstance().viewGame = initializeForView(MyPoker.game, config);

			UtilityGettor._instance = new UtilityAndroid();
			UtilityGettor.initialize();
			UtilityGettor.inst().setActivity(this);

		} else {
			((ViewGroup) ViewGame.getInstance().viewGame.getParent()).removeView(ViewGame.getInstance().viewGame);
		}
		layout.addView(ViewGame.getInstance().viewGame);
		nameUser = new EditText(V2PlayPortalActivity.this);
		layout.addView(nameUser);
		nameRoom = new EditText(V2PlayPortalActivity.this);
		layout.addView(nameRoom);
		passUser = new EditText(V2PlayPortalActivity.this);
		layout.addView(passUser);
		chat = new EditText(V2PlayPortalActivity.this);
		layout.addView(chat);
		gold = new EditText(V2PlayPortalActivity.this);
		layout.addView(gold);
		xu = new EditText(V2PlayPortalActivity.this);
		layout.addView(xu);

		passUser.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		nameUser.setSingleLine(true);
		passUser.setSingleLine(true);
		nameUser.setVisibility(View.INVISIBLE);
		passUser.setVisibility(View.INVISIBLE);
		nameRoom.setVisibility(View.INVISIBLE);
		chat.setVisibility(View.INVISIBLE);
		gold.setVisibility(View.INVISIBLE);
		xu.setVisibility(View.INVISIBLE);
		nameRoom.setTextSize(14);
		// DateFormat.getTimeFormat(this).get
		layout.addView(loadingLayout);

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		// super.onBackPressed();
		if (ViewGame.getInstance().inGame) {
			MyPoker.game.onBackPress();
		} else {
			ViewGame.getInstance().inGame = false;
			ViewGame.getInstance().initGame = false;
			ViewGame.getInstance().game = null;
			ViewGame.instance = null;
			// finish();
			UtilityGettor.inst().setActivity(null);
			super.onBackPressed();
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		// ViewGame.getInstance().instance = null;

		super.onDestroy();
	}

	public EditText makeTextField(final String type, final String content, final float wide, final float height, final float marginTop, final float marginLeft) {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (type == NameEdit.NAME_USER) {
					Log.i(tag, " con me no ");
					heightText = nameUser.getHeight();
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) wide, heightText);
					params.topMargin = (int) (marginTop + (height - heightText) / 2);
					params.leftMargin = (int) marginLeft;

					nameUser.setLayoutParams(params);
					nameUser.setBackgroundResource(0);
					nameUser.setText(content);
					nameUser.setVisibility(View.VISIBLE);

					// nameUser.setBackgroundResource(R.drawable.text_box_grey);

				} else if (type == NameEdit.PASS_USER) {
					heightText = passUser.getHeight();
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) wide, heightText);
					params.topMargin = (int) (marginTop + (height - heightText) / 2);
					params.leftMargin = (int) marginLeft;
					passUser.setLayoutParams(params);
					passUser.setBackgroundResource(0);
					passUser.setText(content);
					passUser.setVisibility(View.VISIBLE);
					passUser.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					// passUser.setBackgroundResource(R.drawable.text_box_grey);
				} else if (type == NameEdit.MAKE_ROOM) {
					heightText = passUser.getHeight();
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) wide, heightText);
					params.topMargin = (int) (marginTop + (height - heightText) / 2);
					params.leftMargin = (int) marginLeft;
					nameRoom.setLayoutParams(params);
					nameRoom.setBackgroundResource(0);
					nameRoom.setText(content);
					nameRoom.setVisibility(View.VISIBLE);
					// passUser.setBackgroundResource(R.drawable.text_box_grey);
				} else if (type == NameEdit.CHAT) {
					heightText = chat.getHeight();
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) wide, heightText);
					params.topMargin = (int) (marginTop + (height - heightText) / 2);
					params.leftMargin = (int) marginLeft;
					chat.setLayoutParams(params);
					chat.setBackgroundResource(0);
					chat.setText(content);
					chat.setVisibility(View.VISIBLE);
					// passUser.setBackgroundResource(R.drawable.text_box_grey);
				} else if (type == NameEdit.GOLD) {
					heightText = gold.getHeight();
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) wide, heightText);
					params.topMargin = (int) (marginTop + (height - heightText) / 2);
					params.leftMargin = (int) marginLeft;
					gold.setLayoutParams(params);
					// gold.setBackgroundResource(0);
					gold.setText(content);
					gold.setVisibility(View.VISIBLE);
					// passUser.setBackgroundResource(R.drawable.text_box_grey);
				} else if (type == NameEdit.XU) {
					heightText = xu.getHeight();
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) wide, heightText);
					params.topMargin = (int) (marginTop + (height - heightText) / 2);
					params.leftMargin = (int) marginLeft;
					xu.setLayoutParams(params);
					// xu.setBackgroundResource(0);
					xu.setText(content);
					xu.setVisibility(View.VISIBLE);
					// passUser.setBackgroundResource(R.drawable.text_box_grey);
				}
			}
		});

		if (type.equals(NameEdit.NAME_USER))
			return nameUser;

		else if (type.equals(NameEdit.PASS_USER))
			return passUser;
		else if (type.equals(NameEdit.MAKE_ROOM))
			return nameRoom;
		else if (type.equals(NameEdit.CHAT))
			return chat;
		else if (type.equals(NameEdit.GOLD))
			return gold;
		else if (type.equals(NameEdit.XU))
			return xu;
		return nameRoom;

	}

	public void hideTextField(final String type) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				EditText editText = ((UtilityAndroid) UtilityGettor.inst()).arrayEditText.get(type);
				if (editText != null)
					editText.setVisibility(View.INVISIBLE);
			}
		});
	}

	public void visibleTextField(final String type) {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				EditText editText = ((UtilityAndroid) UtilityGettor.inst()).arrayEditText.get(type);
				if (editText != null)
					editText.setVisibility(View.VISIBLE);
			}
		});
	}

	public void setTextField(final String type, final String content) {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				EditText editText = ((UtilityAndroid) UtilityGettor.inst()).arrayEditText.get(type);
				if (editText != null)
					editText.setVisibility(View.VISIBLE);
				if (type.equals(NameEdit.PASS_USER))
					passUser.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
		});
	}

	private class StartTask extends TimerTask {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					layout.removeView(loadingLayout);
					view.bringToFront();
					view.start();
					view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

						@Override
						public void onCompletion(MediaPlayer mp) {

							V2PlayPortalActivity.this.layout.removeView(view);
							ViewGame.getInstance().inGame = true;
						}
					});

				}
			});

		}
	}

	public void endGame() {
		ViewGame.getInstance().inGame = false;
		ViewGame.getInstance().initGame = false;
		ViewGame.getInstance().game = null;
		ViewGame.instance = null;
		// finish();
		UtilityGettor.inst().setActivity(null);
		super.onBackPressed();
	}

}